<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="售后单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['supportNo']" placeholder="请输入售后单号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="售后类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['supportType']" :trigger-change="true" dictCode="" placeholder="请选择售后类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="订单id" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['orderId']" placeholder="请输入订单id"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="售后状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['supportStatus']" :trigger-change="true" dictCode="" placeholder="请选择售后状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="审核不通过原因" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['examineRemark']" placeholder="请输入审核不通过原因"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="保修类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['guaranteeType']" :trigger-change="true" dictCode="" placeholder="请选择保修类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="故障类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['faultType']" :trigger-change="true" dictCode="" placeholder="请选择故障类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="故障详情" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['faultRemark']" placeholder="请输入故障详情"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否转让" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['isConvey']" :trigger-change="true" dictCode="" placeholder="请选择是否转让" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="转让人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['transferorUser']" :trigger-change="true" dictCode="" placeholder="请选择转让人" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="上门/维修费用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['repairFee']" placeholder="请输入上门/维修费用" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="配件费用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['fittingFee']" placeholder="请输入配件费用" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="合计维修费用" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['totalFee']" placeholder="请输入合计维修费用" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="维修类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['maintenanceType']" :trigger-change="true" dictCode="" placeholder="请选择维修类型" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="是否寄回" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['isReturn']" :trigger-change="true" dictCode="" placeholder="请选择是否寄回" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="退回接收人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['returnUser']" placeholder="请输入退回接收人"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="退回收货时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['returnTime']" placeholder="请输入退回收货时间"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="处理方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['processingMode']" :trigger-change="true" dictCode="" placeholder="请选择处理方式" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发货方式" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sendType']" placeholder="请输入发货方式"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发货单号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sendOn']" placeholder="请输入发货单号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="发货时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择发货时间" v-decorator="['sendTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="收货人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiver']" placeholder="请输入收货人"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="收货人电话" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['receiverTel']" placeholder="请输入收货人电话"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="市" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['city']" placeholder="请输入市"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="省" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['province']" placeholder="请输入省"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['address']" placeholder="请输入地址"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="签到地址" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['signAddress', validatorRules.signAddress]" placeholder="请输入签到地址"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="签到时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择签到时间" v-decorator="['signTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="经纬度" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['location']" placeholder="请输入经纬度"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="上门时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择上门时间" v-decorator="['visitTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="解决方案" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['solution']" placeholder="请输入解决方案"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="完成时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择完成时间" v-decorator="['finishTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="结单备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['remark']" placeholder="请输入结单备注"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"

  export default {
    name: 'BizOrderSupportForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          signAddress: {
            rules: [
              { required: true, message: '请输入签到地址!'},
            ]
          },
        },
        url: {
          add: "/support/bizOrderSupport/add",
          edit: "/support/bizOrderSupport/edit",
          queryById: "/support/bizOrderSupport/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'supportNo','supportType','orderId','supportStatus','examineRemark','createBy','guaranteeType','faultType','faultRemark','isConvey','transferorUser','conveyCount','repairFee','fittingFee','totalFee','maintenanceType','isReturn','returnUser','returnTime','processingMode','sendType','sendOn','sendTime','receiver','receiverTel','city','province','address','signAddress','signTime','location','visitTime','solution','finishTime','remark'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'supportNo','supportType','orderId','supportStatus','examineRemark','createBy','guaranteeType','faultType','faultRemark','isConvey','transferorUser','conveyCount','repairFee','fittingFee','totalFee','maintenanceType','isReturn','returnUser','returnTime','processingMode','sendType','sendOn','sendTime','receiver','receiverTel','city','province','address','signAddress','signTime','location','visitTime','solution','finishTime','remark'))
      },
    }
  }
</script>